import api from './tool/requestModel.js';
let app = getApp()
   function user_login(data,avatar,name) {
		api.user_login({"open_id":data,"app_id":"tt687924844876863140","avatar":avatar,"name":name})
			.then(res => {
                tt.setStorageSync('userId', res.data.user_id)
                tt.setStorageSync('is_manage', res.data.is_manage)
                tt.setStorageSync('bind_promote', res.data.bind_promote)
                app.globalData.bind_promote = res.data.bind_promote
			}) 
	}
function getUserInfo(openid) {
	tt.getSetting({
		success(res) {
			if (res.authSetting['scope.userInfo']) {
				// 如果已授权,直接获取对应参数
				tt.getUserInfo({
					success(res) {
                        user_login(openid,res.userInfo.avatarUrl,res.userInfo.nickName)
						tt.setStorageSync('userImg', res.userInfo.avatarUrl)
						tt.setStorageSync('userName', res.userInfo.nickName)
					}
				})
			} else if (!res.authSetting['scope.userInfo']) {
				// 说明此时要获取的位置功能尚未授权,
				// 则设置进入页面时主动弹出，直接授权
				tt.authorize({
					scope: 'scope.userInfo',
					success(res) {
						// 授权成功 
						// 成功后获取对应的位置参数
						tt.getUserInfo({
							success(res) {
                                user_login(openid,res.userInfo.avatarUrl,res.userInfo.nickName)
								tt.setStorageSync('userImg', res.userInfo.avatarUrl)
								tt.setStorageSync('userName', res.userInfo.nickName)
                                
							}
						})
					},
					fail() {
						tt.showModal({
							content: '检测到您没打开获取信息功能权限，是否去设置打开？',
							confirmText: "确认",
							cancelText: '取消',
							success: (res) => {
								console.log(res)
								if (res.confirm) {
									tt.openSetting({
										success: (res) => {
										},
										fail: (err) => {
										}
									})
								} else {
									console.log('取消');
								}
							}
						})
					}
				})
			}
		},
		fail() {

		}
	})
}


export default getUserInfo
